package com.merelyb.controller;

import com.merelyb.business.service.account.BusinessAccountInfoService;
import com.merelyb.data.service.service.mybatis.bean.account.AccountInfo;
import com.merelyb.util.MD5Util;
import com.merelyb.util.VerificationUtils;
import com.sun.tools.internal.ws.wsdl.document.http.HTTPConstants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.regex.Pattern;

/**
 * Created by LiM on 2017/12/9.
 */
@Controller
public class IndexController {
    @Autowired
    BusinessAccountInfoService businessAccountInfoService;

    /**
     * 登录页
     * @return
     */
    @RequestMapping("index.do")
    public String getIndex(){
        return "JSP/mainFrame";
    }

    /**
     * 注册页
     * @return
     */
    @RequestMapping("register.do")
    public String getRegister(){
        return "JSP/register";
    }

    /**
     * 忘记密码
     * @return
     */
    @RequestMapping("forgot.do")
    public String getForgot(){
        return "JSP/forgot";
    }

    /**
     * 新增用户
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "newAccount.do")
    public String registNewAccount(HttpServletRequest request, HttpServletResponse response){

        String sPhone = request.getParameter("phone")!= null? request.getParameter("phone"):"";
        String sPwd = request.getParameter("pwd")!= null? request.getParameter("pwd"):"";
        if(sPhone.equals("")){
            request.setAttribute("message", Const.ERROR_PHONE_NULL);
            return "JSP/register";
        }
        VerificationUtils verificationUtils =new VerificationUtils();
        if(!verificationUtils.checkMobile(sPhone)){
            request.setAttribute("message", Const.ERROR_PHONE_VAILD);
            return "JSP/register";
        }
        if(sPwd.equals("")){
            request.setAttribute("message", Const.ERROR_PWD_NULL);
            return "JSP/register";
        }
        if(sPwd.length()< 8 || sPwd.length() > 18){
            request.setAttribute("message", Const.ERROR_PWD_VAILD);
            return "JSP/register";
        }

        String sReturn = "";
        AccountInfo accountInfo = new AccountInfo();
        accountInfo.setCreateTime(new Date());
        accountInfo.setIsDelete(0);
        accountInfo.setTelNum(Long.parseLong(sPhone));
        accountInfo.setUserPwd(MD5Util.string2MD5(sPwd));
        switch (businessAccountInfoService.addNewAccountInfo(accountInfo)){
            case 0:
            {
                request.setAttribute("phone", sPhone);
                sReturn = "JSP/index";
                break;
            }
            case 1:
            {
                request.setAttribute("message", Const.ERROR_PHONE_HAD);
                sReturn = "JSP/register";
                break;
            }
            case -1:
            {
                request.setAttribute("message", Const.ERROR_ACCOUNT_FAIL);
                sReturn = "JSP/register";
                break;
            }
        }
        return sReturn;
    }
}
